import http from '../../apis/api.js';
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    repairType:'',
    type:'',
    pid:'',
    allMaterialParts:[],
    formData:{
      wechatMaterialName:'',
      wechatMaterial:'',
      nums:1,
      maxNum:1
    },
    error:{
      wechatMaterialName:'',
      nums:''
    },
    submitDisable: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self= this;
    console.log(options);
    self.data.id = options.id;
    self.data.repairType = options.repairType;
    self.data.pid = options.repairType !== 'self' ? options.pid:'';
    wx.setNavigationBarTitle({
      title:  '新增换上的零件或备件'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取所有备件库和零件库*/ 
  getAllMaterialParts(data){
    http.allMaterialParts(data).then(res=>{
      self.data.allMaterialParts = res.content;
      self.setData({
        allMaterialParts: self.data.allMaterialParts
      })
    })
  },
  selectItem(e) {
    let selectContent = e.detail;
    self.data.formData.wechatMaterialName = selectContent.name;
    self.data.formData.wechatMaterial = selectContent.id;
    self.data.formData.maxNum = selectContent.remain;
    self.data.formData.type = selectContent.type;
    self.setData({
      formData:self.data.formData
    })

  },
  getSearchAll(e){
    var postData ={
      keywords: e.detail
    }
    if (self.data.repairType!=='self'){
      postData.pid = self.data.pid
    }
    self.getAllMaterialParts(postData)
  },
  /**获取input框的值*/
  getValus(e) {
    let value = e.detail.value
    switch (e.currentTarget.dataset.type) {
      case 'nums':
        self.data.formData.nums = value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'wechatMaterialName':
        if (self.data.formData.wechatMaterialName === '') {
          self.data.error.wechatMaterialName = "备件或零件"
          return false
        }
        self.data.error.wechatMaterialName = '';
        return true;
        break;
      case 'nums':
        if (self.data.formData.nums === '') {
          self.data.error.nums = "数量不能为空"
          return false
        }
        if (self.data.formData.nums <= 0) {
          self.data.error.nums = "数量不能小于等于0"
          return false
        }
        if (self.data.formData.nums > self.data.formData.maxNum) {
          self.data.error.nums = "数量大于库存";
          return false
        }
        self.data.error.nums = '';
        return true;
        break;
    }
  },
  /**点击提交*/
  submitInfo() {
    self.setData({
      submitDisable: true
    })
    if (self.checked('wechatMaterialName') && self.checked('nums')) {
      let postData = {
        pid: self.data.id,
        nums: self.data.formData.nums,
        type:self.data.formData.type
      };
      self.data.formData.type === 1 ? postData.wechatMaterial = self.data.formData.wechatMaterial : postData.wechatParts = self.data.formData.wechatMaterial;
      switch (self.data.repairType){
        case 'self':
          http.insertSelfRepairUse(postData).then(res => {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000,
              success() {
                wx.navigateBack();
              },
              complete() {
                self.setData({
                  submitDisable: false
                })
              }
            })
          }, error => {
            self.setData({
              submitDisable: false
            })
          })
        break;
        case 'send':
          http.insertSendRepairUse(postData).then(res => {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000,
              success() {
                wx.navigateBack();
              },
              complete() {
                self.setData({
                  submitDisable: false
                })
              }
            })
          }, error => {
            self.setData({
              submitDisable: false
            })
          })
        break;
        case 'maintain':
          http.insertMaintainRepairUse(postData).then(res => {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 2000,
              success() {
                wx.navigateBack();
              },
              complete() {
                self.setData({
                  submitDisable: false
                })
              }
            })
          }, error => {
            self.setData({
              submitDisable: false
            })
          })
        break;
      }
    } else {
      self.setData({
        error: self.data.error,
        submitDisable: false
      })
    }
  },
})